clear all 
set more off 
set maxvar 15000 
clear matrix


*******************************************
* IGE AND RANK, 12 LEAST TOP-CODED SURVEYS
*******************************************

	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    tab non_topcod_sample if baseline_sample==1, m
    tab data if non_topcod_sample==1
    tab data if non_topcod_sample==. & baseline_sample==1
    
    foreach var in ige rank  {
    preserve
    estimates clear 
        
        forval i=1(1)3 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
    foreach variation in 1 2 3 {

        if "`var'"=="ige" & "`variation'"=="3"  continue 

    * Dependent variables
        if "`var'"=="ige" & "`variation'"=="1" local dep "log_son_baseline"
        if "`var'"=="ige" & "`variation'"=="2" local dep "log_son_baseline"
        
        if "`var'"=="rank" & "`variation'"=="1" local dep "rank_son_baseline"
        if "`var'"=="rank" & "`variation'"=="2" local dep "rank_son_baseline"
        if "`var'"=="rank" & "`variation'"=="3" local dep "rank_son_baseline_nontopcod"
        
    * Independent variables
        if "`var'"=="ige" & "`variation'"=="1" local indep "log_father_baseline"
        if "`var'"=="ige" & "`variation'"=="2" local indep "log_father_baseline"
        
        if "`var'"=="rank" & "`variation'"=="1" local indep "rank_father_baseline"
        if "`var'"=="rank" & "`variation'"=="2" local indep "rank_father_baseline"
        if "`var'"=="rank" & "`variation'"=="3" local indep "rank_father_baseline_nontopcod"
        
    * Sample and weight
        if "`variation'"=="1" local samp1 "& baseline_sample==1 [pw=wgt_sex_race]"
        if "`variation'"=="2" local samp1 "& non_topcod_sample==1 [pw=wgt_sex_race]"
        if "`variation'"=="3" local samp1 "& non_topcod_sample==1 [pw=wgt_sex_race]"
        
        
        levelsof decade, local(decades)
        
    *1. 
        foreach x of local decades {
        eststo : quietly reg `dep' `indep'  if decade==`x' `samp1', robust
            replace est_`variation' = _b[`indep'] if decade==`x'
            replace est_ub_`variation' = _b[`indep']+1.96*_se[`indep'] if decade==`x'
            replace est_lb_`variation' = _b[`indep']-1.96*_se[`indep'] if decade==`x'
            }

    }
        
    * Figures
        bysort decade: keep if _n==1
        keep decade est_*
        
        reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
        replace decade= decade+1.5 if estimate==2   
        if "`var'"=="rank" replace decade= decade+3 if estimate==3
        
        if "`var'"=="ige" {
                local TITLE "IGE"
                local ub "1"
                local lb "0"
                local skip "0.25"
                local maincolor "midblue"
                local color2 "navy"
                local m "8"
            }

        if "`var'"=="rank" {
                local TITLE "Rank"
                local ub "0.45"
                local lb "0.15"
                local skip "0.1"
                local maincolor "orange"
                local color2 "cranberry"
                local color3 "pink*0.75"
                local m "8"
        }
            
        if "`var'"=="ige" {
            
            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(`maincolor') msize(medium) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(`maincolor') lwidth(0.5))
                    (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(`color2') msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(`color2') lwidth(0.5) )
                   ,
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            ylabel(`lb'(`skip')`ub', axis(1)) yti("`TITLE' coefficient" " ", axis(1)) 
            legend(on ring(0) pos(`m') rows(3) order(1 "Baseline inc. & sample" 3 "Baseline inc. & LTC sample"))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
            #delimit cr
            graph export "$Mydirectory2/appendix_b/`TITLE'_overtime_non_topcod_sample.pdf", as(pdf) replace 
        }
                
        if "`var'"=="rank" {
            
            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(`maincolor') msize(medium) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(`maincolor') lwidth(0.5))
                   (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(`color2') msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(`color2') lwidth(0.5) )
                   (scatter est_ decade if estimate==3,  msymbol(triangle) mcolor(`color3') msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(`color3') lwidth(0.5) )
                   ,
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            ylabel(`lb'(`skip')`ub', axis(1)) yti("`TITLE' coefficient" " ", axis(1)) 
            legend(on ring(0) pos(`m') rows(3) order(1 "Baseline inc. & sample" 3 "Baseline inc. & LTC sample" 5 "LTC inc. & sample"))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
            #delimit cr
            graph export "$Mydirectory2/appendix_b/`TITLE'_overtime_non_topcod_sample.pdf", as(pdf) replace
        
        }

    restore
    }
